package com.zhy.springcloud.service.impl;

import com.zhy.springcloud.dao.OrderDao;
import com.zhy.springcloud.domain.Order;
import com.zhy.springcloud.service.AccountService;
import com.zhy.springcloud.service.OrderService;
import com.zhy.springcloud.service.StorageService;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.web.servlet.filter.OrderedHiddenHttpMethodFilter;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @Author zhy
 * @Description
 * @ClassName OrderServiceImpl
 * @Date 2021/1/7 15:20
 **/
@Service
@Slf4j
public class OrderServiceImpl implements OrderService {

    @Resource
    private OrderDao orderDao;

    @Resource
    private AccountService accountService;

    @Resource
    private StorageService storageService;

    @Override
    @GlobalTransactional(name = "fsp_create_order",rollbackFor = Exception.class)
    public void create(Order order) {
        log.info("----->开始新建订单");
        orderDao.create(order);

        log.info("----->订单微服务开始调用库存,做扣减Count");
        storageService.decrease(order.getProductId(),order.getCount());
        log.info("----->订单微服务开始调用库存,做扣减End");

        log.info("----->订单微服务开始调用账户,做扣减Money");
        accountService.decrease(order.getUserId(),order.getMoney());
        log.info("----->订单微服务开始调用账户,做扣减End");

        log.info("----->修改订单状态开始");
        orderDao.update(order.getUserId(),0);
        log.info("----->下订单结束了,O(∩_∩)O哈哈~");

    }
}
